package com.genghis.ptas.note.dao.impl;

import com.genghis.core.page.dao.PageDao;
import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.note.dao.PublicityNoteDao;
import com.genghis.ptas.note.entity.Note;
import com.genghis.ptas.note.entity.PublicityNote;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Repository("publicityNoteDao")
public class PublicityNoteDaoImpl extends PageDao implements PublicityNoteDao {

    @Override
    public Page<PublicityNote> findPublicityNotePageList(PageParameter pageParameter, Note note) {
        StringBuilder sql = new StringBuilder("SELECT u.user_name ,p.note_id,p.state,p.publicity_date,p.id FROM note n,publicity_note p,sys_user u WHERE n.id=p.note_id and n.user_id=u.id ");
        if (!"".equals(note.getNoteName())) {
            sql.append("AND n.note_name like '%").append(note.getNoteName().trim()).append("%'");
        }
        if ("".equals(note.getCreateDate())) {
            sql.append("AND p.publicity_date = '").append(note.getCreateDate()).append("'");
        }
        if (note.getUserId() != 0) {
            sql.append("AND note_type =").append(note.getNoteType());
        }
        return queryForPage(pageParameter, sql.toString(), new BeanPropertySqlParameterSource(note), new BeanPropertyRowMapper<PublicityNote>(PublicityNote.class));
    }

    @Override
    public int addPublicityNote(PublicityNote publicityNote) {
        String sql = "INSERT INTO publicity_note (note_id,state,publicity_date) VALUES(:noteId, :state, :publicityDate)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(publicityNote), keyHolder);
        return keyHolder.getKey().intValue();
    }

    @Override
    public void auditNotes(String ids, int state) {
        String sql = "UPDATE publicity_note SET state = :state WHERE id in(" + ids + ")";
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(new PublicityNote(state)));
    }

    @Override
    public PublicityNote findNoteStateById(int id) {
        String sql = "select * from publicity_note p where p.note_id =?";
        List<PublicityNote> publicityNotes = getJdbcTemplate().query(sql, new BeanPropertyRowMapper<PublicityNote>(PublicityNote.class), id);
        return publicityNotes.size() == 0 ? null : publicityNotes.get(0);
    }

    @Override
    public void delPublicityNote(String id) {
        String sql = "DELETE FROM publicity_note WHERE note_id =" + id;
        getJdbcTemplate().update(sql);
    }
}
